package com.jxwifi.cloud.quickclean.popwindow;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.bean.DiscountsBean;
import com.jxwifi.cloud.quickclean.card.CardRollActivity;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.popwindow.BasicPopmodule;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

public class PopPayOrderModule extends BasicPopmodule {

    private String TAG = this.getClass().getSimpleName();
    private Context mContext;
    private PopPayListener mPoplistener;

    private String mAlipayOrWeixin = StateUtils.ALIPAY;

    private String mTotalAmount; //总计金额
    private String mDeposit;//预定金额
    private double mTailMoney;//尾款
    private double mCouponsdiscount; //优惠卷
    private double mPaybalance; //清洁宝余额总数
    private String mServiceCode;
    private String mStart;
    private int mVersion;
    private double mAssemble;
    private List<DiscountsBean> discounts;//立减、折扣优惠
    private double discount;//折扣优惠
    private double minus;//立减优惠

    public PopPayOrderModule(Context context, String totalAmount, String deposit,
                             double tailMoney, double couponsdiscount, String ServiceCode, double Paybalance,String mStart,int version,double Assemble,List<DiscountsBean> discounts, PopPayListener l) {
        this.mContext = context;
        this.mPoplistener = l;
        mTotalAmount = totalAmount;
        this.discounts = discounts;
        mDeposit = deposit;
        mTailMoney = tailMoney;
        this.mCouponsdiscount = couponsdiscount;
        this.mServiceCode = ServiceCode;
        this.mPaybalance = Paybalance;
        this.mStart=mStart;
        this.mVersion=version;
        this.mAssemble=Assemble;
        initView();
    }

    public void getCoupons(double couponsdiscount) {
        this.mCouponsdiscount = couponsdiscount;

    }

    @Override
    public void setPopwindow(PopupWindow pop) {
        mPopwindows = pop;
    }

    @Override
    public View getView() {
        return mContentView;
    }

    private void initView() {
        mContentView = LayoutInflater.from(mContext).inflate(R.layout.pop_pay_order_module, new LinearLayout(mContext), false);
        setListener();
    }

    public void setListener() {
        View mVwPopBlank = (View) mContentView.findViewById(R.id.vw_pop_blank);
        TextView mTvOrderStartsCancelEvaluate = (TextView) mContentView.findViewById(R.id.tv_order_starts_cancel_evaluate);
        final TextView mTvPayOrderPrice = (TextView) mContentView.findViewById(R.id.tv_pay_order_price);
        TextView mTvCleaningPrice = (TextView) mContentView.findViewById(R.id.tv_cleaning_price);
        TextView mTvPaidDeposit = (TextView) mContentView.findViewById(R.id.tv_paid_deposit);
        TextView mTvPayCoupon = (TextView) mContentView.findViewById(R.id.tv_pay_coupon);
        RelativeLayout mRelPayCoupon = (RelativeLayout) mContentView.findViewById(R.id.rel_pay_coupon);//优惠卷
        RelativeLayout mRelPayCoupon1 = (RelativeLayout) mContentView.findViewById(R.id.rel_pay_coupon1);//支付宝支付
        //final ImageView mImgPayOrderOfAlipay = (ImageView) mContentView.findViewById(R.id.img_pay_order_of_alipay);
        final CheckBox mImgPayOrderOfAlipay = (CheckBox) mContentView.findViewById(R.id.cb_pay_order_of_alipay);
        RelativeLayout mRelPayOrderOfWeixin = (RelativeLayout) mContentView.findViewById(R.id.rel_pay_order_of_weixin);//微信支付
        //final ImageView mImgPayOrderOfWeixin = (ImageView) mContentView.findViewById(R.id.img_pay_order_of_weixin);
        final CheckBox mImgPayOrderOfWeixin = (CheckBox) mContentView.findViewById(R.id.cb_pay_order_of_weixin);
        TextView mTvPayPayment = (TextView) mContentView.findViewById(R.id.tv_pay_payment);
        TextView mTvPopPayYuePrice = (TextView) mContentView.findViewById(R.id.tv_pop_pay_yue_price);
        TextView mTvPopPayYueTitle=(TextView)mContentView.findViewById(R.id.tv_pop_pay_yue_title);
        RelativeLayout mRelPayPaidDeposit1=(RelativeLayout)mContentView.findViewById(R.id.rel_pay_paid_deposit1);
        TextView mTvPaidDeposit11 = (TextView) mContentView.findViewById(R.id.tv_paid_deposit11);
        RelativeLayout rel_discount=(RelativeLayout)mContentView.findViewById(R.id.rel_discount);
        TextView tv_discount = (TextView) mContentView.findViewById(R.id.tv_discount);
        RelativeLayout rel_minus=(RelativeLayout)mContentView.findViewById(R.id.rel_minus);
        TextView tv_minus = (TextView) mContentView.findViewById(R.id.tv_minus);
        DecimalFormat format = new DecimalFormat("#0.00");
        //format.format(mTailMoney)
        mTvPaidDeposit.setText("\u00A5-" + format.format(Double.valueOf(mDeposit)));
        mTvCleaningPrice.setText("\u00A5 " + format.format(Double.valueOf(mTotalAmount)));
        if(mAssemble != 0){
            mRelPayPaidDeposit1.setVisibility(View.VISIBLE);
        }
        for (int i = 0; i < discounts.size(); i++) {
            if(discounts.get(i).getDescribeType().equals("PERCENTAGE")){//折扣优惠
                rel_discount.setVisibility(View.VISIBLE);
                discount = Double.valueOf(mTotalAmount)*(10-discounts.get(i).getPrice())/10;
                tv_discount.setText("\u00A5-"+format.format(discount));
            }else if(discounts.get(i).getDescribeType().equals("AMOUNT")){//立减优惠
                rel_minus.setVisibility(View.VISIBLE);
                tv_minus.setText("\u00A5-"+format.format(discounts.get(i).getPrice()));
                minus = discounts.get(i).getPrice();
            }
            rel_discount.setVisibility(View.VISIBLE);
            mRelPayCoupon.setVisibility(View.GONE);//折扣与优惠券不能同时享用
            mCouponsdiscount = 0.00;
            mTailMoney = mTailMoney - discount - minus;
        }

        //判断尾款是否为0
        if (mTailMoney != 0.00) {
            //优惠卷抵扣
            if (mCouponsdiscount != 0.00) {
                mRelPayCoupon.setVisibility(View.VISIBLE);
                mTvPayCoupon.setText("\u00A5-" +  format.format(mCouponsdiscount));
                if (StateUtils.NORMAL.equals(mStart)){
                    //尾款减去优惠券
                    if (mTailMoney - mCouponsdiscount > 0) {
                        BigDecimal b = new BigDecimal((mTailMoney - mCouponsdiscount));//金额进行精确计算
                        double DifferencePrice = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                        if (mPaybalance>=DifferencePrice){
                            if (mVersion==1){
                                mRelPayPaidDeposit1.setVisibility(View.VISIBLE);
                                mTvPaidDeposit11.setText("\u00A5 "+mAssemble);
                                mTvPayOrderPrice.setText("\u00A5 0.00");
                                mTvPopPayYueTitle.setText("余额已抵扣"+format.format(DifferencePrice-mAssemble)+"元");
                                mTvPopPayYuePrice.setText("(剩余:" +format.format(mPaybalance - DifferencePrice) + "元)");
                            }else{
                                mRelPayPaidDeposit1.setVisibility(View.GONE);
                                mTvPayOrderPrice.setText("\u00A5 0.00");
                                mTvPopPayYueTitle.setText("余额已抵扣"+format.format(DifferencePrice)+"元");
                                mTvPopPayYuePrice.setText("(剩余:" +format.format(mPaybalance - DifferencePrice) + "元)");
                            }

                            mAlipayOrWeixin = StateUtils.BALANCE;
                            mImgPayOrderOfWeixin.setChecked(false);
                            mImgPayOrderOfAlipay.setChecked(false);
                        }else{
                            if (mVersion==1){
                                mRelPayPaidDeposit1.setVisibility(View.VISIBLE);
                                mTvPaidDeposit11.setText("\u00A5 "+mAssemble);
                                mTvPayOrderPrice.setText("\u00A5 "+format.format(DifferencePrice - mPaybalance-mAssemble));
                                mTvPopPayYueTitle.setText("余额已抵扣" + format.format(mPaybalance) + "元");
                                mTvPopPayYuePrice.setText("(剩余:0.00元)");
                            }else{
                                mRelPayPaidDeposit1.setVisibility(View.GONE);
                                mTvPayOrderPrice.setText("\u00A5 "+format.format(DifferencePrice - mPaybalance));
                                mTvPopPayYueTitle.setText("余额已抵扣" + format.format(mPaybalance) + "元");
                                mTvPopPayYuePrice.setText("(剩余:0.00元)");
                            }

                            mAlipayOrWeixin = StateUtils.WEIXIN;
                            mImgPayOrderOfWeixin.setChecked(true);
                            mImgPayOrderOfAlipay.setChecked(false);
                        }
                    } else {
                        mTvPayOrderPrice.setText("\u00A5 0.00");
                        mTvPopPayYueTitle.setText("余额已抵扣0.00元");
                        mTvPopPayYuePrice.setText("(剩余:"+format.format(mPaybalance)+"元)");
                        mAlipayOrWeixin = StateUtils.PAY_ZERO;
                        //设置按钮为false
                        mImgPayOrderOfAlipay.setChecked(false);
                        mImgPayOrderOfWeixin.setChecked(false);
                        //设置不可点击
                        mImgPayOrderOfAlipay.setClickable(false);
                        mImgPayOrderOfWeixin.setClickable(false);
                    }
                }else{

                    BigDecimal b = new BigDecimal((mTailMoney - mCouponsdiscount));//金额进行精确计算
                    double DifferencePrice = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    mTvPayOrderPrice.setText("\u00A5 "+format.format(DifferencePrice));
                    mTvPopPayYueTitle.setText("该账户已被冻结，请联系我们");
                    mTvPopPayYuePrice.setText("");

                    mAlipayOrWeixin = StateUtils.WEIXIN;
                    mImgPayOrderOfWeixin.setChecked(true);
                    mImgPayOrderOfAlipay.setChecked(false);
                }
            } else {
                mRelPayCoupon.setVisibility(View.GONE);
                if (StateUtils.NORMAL.equals(mStart)){
                    if (mPaybalance>=mTailMoney){
                        if (mVersion==1){
                            mRelPayPaidDeposit1.setVisibility(View.VISIBLE);
                            mTvPaidDeposit11.setText("\u00A5 "+mAssemble);
                            mTvPayOrderPrice.setText("\u00A5 0.00");
                            double mNewPrices=mTailMoney-mAssemble;
                            mTvPopPayYueTitle.setText("余额已抵扣"+format.format(mNewPrices)+"元");
                            mTvPopPayYuePrice.setText("(剩余:" +format.format(mPaybalance - mNewPrices) + "元)");
                        }else{
                            mRelPayPaidDeposit1.setVisibility(View.GONE);
                            mTvPayOrderPrice.setText("\u00A5 0.00");
                            mTvPopPayYueTitle.setText("余额已抵扣"+format.format(mTailMoney)+"元");
                            mTvPopPayYuePrice.setText("(剩余:" +format.format(mPaybalance - mTailMoney) + "元)");
                        }
                        mAlipayOrWeixin = StateUtils.BALANCE;
                        mImgPayOrderOfWeixin.setChecked(false);
                        mImgPayOrderOfAlipay.setChecked(false);
                    }else{
                        if (mVersion==1){
                            mRelPayPaidDeposit1.setVisibility(View.VISIBLE);
                            mTvPaidDeposit11.setText("\u00A5 "+mAssemble);
                            mTvPayOrderPrice.setText("\u00A5 "+format.format(mTailMoney - mPaybalance-mAssemble));
                            if (mTailMoney - mPaybalance-mAssemble>0){
                                mTvPopPayYueTitle.setText("余额已抵扣" + format.format(mPaybalance) + "元");
                                mTvPopPayYuePrice.setText("(剩余:"+format.format(mPaybalance - mTailMoney-mAssemble) +"元)");
                            }else{
                                mTvPopPayYuePrice.setText("(剩余:0.00元)");
                            }
                        }else{
                            mRelPayPaidDeposit1.setVisibility(View.GONE);
                            mTvPayOrderPrice.setText("\u00A5 "+format.format(mTailMoney - mPaybalance));
                            mTvPopPayYueTitle.setText("余额已抵扣" + format.format(mPaybalance) + "元");
                            mTvPopPayYuePrice.setText("(剩余:0.00元)");
                        }
                        mAlipayOrWeixin = StateUtils.WEIXIN;
                        mImgPayOrderOfWeixin.setChecked(true);
                        mImgPayOrderOfAlipay.setChecked(false);
                    }
                }else{
                    BigDecimal b = new BigDecimal(mTailMoney);//金额进行精确计算
                    double DifferencePrice = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    mTvPayOrderPrice.setText("\u00A5 "+format.format(DifferencePrice));
                    mTvPopPayYueTitle.setText("该账户已被冻结，请联系我们");
                    mTvPopPayYuePrice.setText("");

                    mAlipayOrWeixin = StateUtils.WEIXIN;
                    mImgPayOrderOfWeixin.setChecked(true);
                    mImgPayOrderOfAlipay.setChecked(false);
                }

            }
            /*//尾款减去所有优惠08.21
            if(mPaybalance>= (mTailMoney-mCouponsdiscount-mAssemble-discount-minus)){
                mTvPayOrderPrice.setText("\u00A5 0.00");
                mTvPopPayYueTitle.setText("余额已抵扣"+format.format(mTailMoney-mCouponsdiscount-mAssemble-discount-minus)+"元");
                mTvPopPayYuePrice.setText("(剩余:" +format.format(mPaybalance - (mTailMoney-mCouponsdiscount-mAssemble-discount-minus)) + "元)");
            }else {
                mTvPayOrderPrice.setText("\u00A5 "+format.format(mTailMoney-mCouponsdiscount-mAssemble-discount-minus-mPaybalance));
                mTvPopPayYueTitle.setText("余额已抵扣"+format.format(mPaybalance)+"元");
                mTvPopPayYuePrice.setText("(剩余:0.00元)");
            }*/

        } else {
            mRelPayCoupon.setVisibility(View.GONE);
            mTvPayOrderPrice.setText("\u00A5 0.00");
            mTvPopPayYueTitle.setText("余额已抵扣0.00元");
            mTvPopPayYuePrice.setText("(剩余:"+format.format(mPaybalance) +"元)");
            mAlipayOrWeixin = StateUtils.PAY_ZERO;
            //设置按钮为false
            mImgPayOrderOfAlipay.setChecked(false);
            mImgPayOrderOfWeixin.setChecked(false);
            //设置不可点击
            mImgPayOrderOfAlipay.setClickable(false);
            mImgPayOrderOfWeixin.setClickable(false);

        }
        mVwPopBlank.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mPopwindows.dismiss();
            }
        });

        mTvOrderStartsCancelEvaluate.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mPopwindows.dismiss();
            }
        });
        mImgPayOrderOfAlipay.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                double mPriceCou = mTailMoney - mCouponsdiscount;
                if (StateUtils.NORMAL.equals(mStart)){
                    if (mPaybalance >= mPriceCou) {
                        Toast toast = Toast.makeText(mContext, "余额已抵扣全额", Toast.LENGTH_SHORT);
                        ToastUtils.showMyToast(toast, 3000);
                        mImgPayOrderOfAlipay.setChecked(false);
                        return;
                    }
                }
                if (mImgPayOrderOfAlipay.isChecked()) {
                    mAlipayOrWeixin = StateUtils.ALIPAY;
                    mImgPayOrderOfWeixin.setChecked(false);
                } else {
                    mAlipayOrWeixin = StateUtils.PAY_NULL;
                }
            }
        });

        mImgPayOrderOfWeixin.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                double mPriceCou = mTailMoney - mCouponsdiscount;
                if (StateUtils.NORMAL.equals(mStart)){
                    if (mPaybalance >= mPriceCou) {
                        Toast toast = Toast.makeText(mContext, "余额已抵扣全额", Toast.LENGTH_SHORT);
                        ToastUtils.showMyToast(toast, 3000);
                        mImgPayOrderOfWeixin.setChecked(false);
                        return;
                    }
                }
                if (mImgPayOrderOfWeixin.isChecked()) {
                    mAlipayOrWeixin = StateUtils.WEIXIN;
                    mImgPayOrderOfAlipay.setChecked(false);
                } else {
                    mAlipayOrWeixin = StateUtils.PAY_NULL;
                }
            }
        });


        mTvPayPayment.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (StateUtils.PAY_NULL.equals(mAlipayOrWeixin)) {
                    Toast toast = Toast.makeText(mContext, "请选择一种支付方式", Toast.LENGTH_SHORT);
                    ToastUtils.showMyToast(toast, 3000);
                    return;
                }
                mPoplistener.getPayOrder(mAlipayOrWeixin, mTvPayOrderPrice.getText().toString());
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPopwindows.dismiss();
                    }
                }, 500);
            }
        });
        mRelPayCoupon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPopwindows.dismiss();
                    }
                }, 500);
                Intent intent = new Intent();
                intent.setClass(mContext, CardRollActivity.class);
                intent.putExtra("parameters", StateUtils.PAY_COME);
                intent.putExtra("service_code", mServiceCode);
                intent.putExtra("money", mTotalAmount);
                mContext.startActivity(intent);
            }
        });
    }

    public interface PopPayListener {
        void getPayOrder(String PayType, String PayPrice);
    }

    public void getBackground(ImageView ImgPayOrderOfAlipay, ImageView ImgPayOrderOfWeixin) {
        ImgPayOrderOfAlipay.setBackgroundResource(R.mipmap.icon_no_checked);
        ImgPayOrderOfWeixin.setBackgroundResource(R.mipmap.icon_no_checked);
    }

    public void getdetuileground(ImageView ImgPayOrderOfAlipay, ImageView ImgPayOrderOfWeixin) {
        ImgPayOrderOfAlipay.setBackgroundResource(R.mipmap.icon_alipay_right);
        ImgPayOrderOfWeixin.setBackgroundResource(R.mipmap.icon_notchecked);
    }
}
